package com.glx.test.service.Impl;

import com.github.pagehelper.PageHelper;
import com.glx.test.dao.ProblemMapper;
import com.glx.test.dto.RecentSubmission;
import com.glx.test.pojo.Problem;
import com.glx.test.pojo.Submission;
import com.glx.test.pojo.TestCase;
import com.glx.test.response.ListResponse;
import com.glx.test.response.ProblemResponse;
import com.glx.test.response.SubmissionResponse;
import com.glx.test.response.SubmissionWithProblemResponse;
import com.glx.test.service.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProblemServiceImpl implements ProblemService {
    @Autowired
    private ProblemMapper problemMapper;

    @Override
    public ProblemResponse createProblem(Problem problem) {
        int i = problemMapper.insertProblem(problem);
        if (i == 1) {
            return new ProblemResponse("success", "创建成功", problem, null);
        } else {
            return new ProblemResponse("fail", "创建失败", problem,null);
        }
    }

    @Override
    public ProblemResponse insertTestCase(TestCase testCase) {
        int i = problemMapper.insertTestCase(testCase);
        if (i == 1) {
            return new ProblemResponse("success", "创建成功", null, testCase);
        } else {
            return new ProblemResponse("fail", "创建失败", null, testCase);
        }
    }

    @Override
    public ListResponse SearchFullProblems(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Problem> problemList = problemMapper.getProblemList();
        return new ListResponse("success", "查询成功", problemList, null);
    }

    @Override
    public ProblemResponse getProblemById(Long id) {
        Problem problemById = problemMapper.getProblemById(id);
        if (problemById != null){
            TestCase testCase = problemMapper.getTestCaseByProblemId(id);
            return new ProblemResponse("success", "查询成功", problemById, testCase);
        }
        return new ProblemResponse("fail", "查询失败", null, null);

    }

    @Override
    public SubmissionWithProblemResponse getSubmissionByUserId(Long user_id) {

        List<RecentSubmission> recentSubmissionsLists=new ArrayList<>();

        // 获取用户提交记录
        List<Submission> submissionByUserId = problemMapper.getFiveSubmissionByUserId(user_id);
        // 遍历提交记录，获取题目信息
        for (Submission submission : submissionByUserId) {
            RecentSubmission recentSubmission = new RecentSubmission();
            Problem problemById = problemMapper.getProblemById(submission.getProblem_id());
            recentSubmission.setTitle(problemById.getTitle());
            recentSubmission.setDifficulty(problemById.getDifficulty().toString());
            recentSubmission.setStatus(submission.getStatus());
            recentSubmission.setCreated_at(submission.getCreated_at().toString());
            recentSubmissionsLists.add(recentSubmission);
        }
        return new SubmissionWithProblemResponse("success", "查询成功", recentSubmissionsLists);

    }

    @Override
    public SubmissionResponse getSubmissionByProblemId(Long problem_id) {
        List<Submission> submissionByProblemId = problemMapper.getSubmissionByProblemId(problem_id);
        return new SubmissionResponse("success", "查询成功", submissionByProblemId);
    }

    @Override
    public SubmissionResponse insertSubmission(Submission submission) {
        int i = problemMapper.insert(submission);
        if (i == 1) {
            return new SubmissionResponse("success", "提交成功", null);
        } else {
            return new SubmissionResponse("fail", "提交失败", null);
        }
    }


}
